#include "MainScene.h"
#include "Helpers/Helpers.h"
#include "StarComponents.h"
#include "Input/InputManager.h"
#include "Graphics/GraphicsManager.h"
#include "Scenes/SceneManager.h"
#include "Sound/SoundService.h"
#include "Assets/FontManager.h"
#include "../Objects/GlobalVariables.h"
#include "MenuScene.h"

#ifdef DESKTOP
#include <glew.h>
#else
#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>
#endif

#define INPUT_MANAGER (star::InputManager::GetInstance())
#define LOGGER (star::Logger::GetInstance())

MainScene::MainScene(const tstring & Name):
	BaseScene(Name),
	m_TotalFrames(0),
	m_FPS(0),
	m_Step(0),
	m_CurrentAnimation(0),
	m_PassedMiliseconds(0),	
	m_pActiveCamera(nullptr),
	m_TapGesture(nullptr),
	m_pBackground(nullptr),
	m_pScreenOverlay(nullptr),
	m_pGridObj(nullptr),
	m_pSelectorObj(nullptr),
	m_pHUD(nullptr),
	m_pGameOverScreen(nullptr),
	m_bIsGameOver(false),
	m_bIsPaused(false),
	m_bFirstTick(true),
	m_CooldownTimer(2.0f)
{
		
}

MainScene::~MainScene()
{

}

void MainScene::CreateObjects()
{
	star::SpriteAnimationManager::GetInstance()->
		AddSpritesheet(_T("Tiles/TileAnimation.xml"),_T("Tiles/TilesAnimation.star"));
	star::SpriteAnimationManager::GetInstance()->
		AddSpritesheet(_T("Player/SelectorAnimation.xml"), _T("Player/SelectorAnimation.star"));

	m_pBackground = new star::Object();
	m_pBackground->AddComponent(new star::SpriteComponent(_T("HUD/Background.png"),_T("BG")));
	AddObject(m_pBackground);

	m_pActiveCamera = new star::BaseCamera();
	AddObject(m_pActiveCamera);

	m_TapGesture = new star::TapGesture();
	m_GestureManagerPtr->AddGesture(m_TapGesture, _T("TapGesture"));
	
	m_pGridObj = new Grid();
	AddObject(m_pGridObj);
	m_pGridObj->GetTransform()->Translate(5,9);
	
	m_pHUD = new HUD();
	AddObject(m_pHUD);

	m_pSelectorObj = new Selector(0,0);
	m_pSelectorObj->SetMaxHorizontalTiles(m_pGridObj->GetNrOfHorizontalTiles());
	m_pSelectorObj->SetMaxVerticalTiles(m_pGridObj->GetSafeZone());
	AddObject(m_pSelectorObj);

	m_pGameOverScreen = new GameOverScreen();
	AddObject(m_pGameOverScreen);
	m_pGameOverScreen->SetDisabled(true);

	m_pPauseScreen = new PauseScreen();
	AddObject(m_pPauseScreen);
	m_pPauseScreen->SetDisabled(true);

	m_pLoadingScreen = new PauseScreen(true);
	AddObject(m_pLoadingScreen);

	m_pScreenOverlay = new star::Object();
	m_pScreenOverlay->AddComponent(new star::SpriteComponent(_T("HUD/ScreenOverlay.png"),_T("Overlay"), true, true));
	AddObject(m_pScreenOverlay);
}

void MainScene::AfterInitializedObjects()
{
	BaseScene::AfterInitializedObjects();
}

void MainScene::Update(const star::Context& context)
{
	SetActiveCamera(m_pActiveCamera);
	if(m_bFirstTick)
	{	
		m_pLoadingScreen->Reverse();
		m_bFirstTick = false;
	}

	if(!m_bIsGameOver)
	{
		if(!m_bIsPaused)
		{
#ifdef _WIN32
			if((INPUT_MANAGER->IsKeyboardKeyDown('W', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('W', false)) ||
				(INPUT_MANAGER->IsKeyboardKeyDown('Z', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('Z', false)) ||
				(INPUT_MANAGER->IsKeyboardKeyDown('X', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('X', false)))
			{
				int x, y;
				m_pSelectorObj->GetCurrentPos(x,y);
				m_pGridObj->SwichtTileWithRightSide(x,y);
			}

			if(m_pGridObj->GameOver())
			{
				m_pGameOverScreen->SetDisabled(false);
				//Freeze selector
				m_pSelectorObj->Freeze(true);
				m_pGridObj->Freeze(true);
				m_bIsGameOver = true;
			}
			if(INPUT_MANAGER->IsKeyboardKeyDown(VK_RETURN, true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown(VK_RETURN, false))
			{
				//Freeze selector
				for(auto object : m_Objects)
				{
					object->Freeze(true);
				}
				m_pPauseScreen->SetDisabled(false);
				m_bIsPaused = true;
			}
		}
		else
		{
			if(INPUT_MANAGER->IsKeyboardKeyDown(VK_RETURN, true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown(VK_RETURN, false))
			{
				for(auto object : m_Objects)
				{
					if(object != m_pGameOverScreen)
					{
						object->Freeze(false);
					}			
				}
				m_pPauseScreen->Reverse();
				m_bIsPaused = false;
			}
			if((INPUT_MANAGER->IsKeyboardKeyDown('W', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('W', false)) ||
				(INPUT_MANAGER->IsKeyboardKeyDown('Z', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('Z', false)))
			{
				ResetGame();
			}
			if(INPUT_MANAGER->IsKeyboardKeyDown('X', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('X', false))
			{
				MenuScene* menuScene = reinterpret_cast<MenuScene*>(star::SceneManager::GetInstance()->GetScene(_T("MenuScene")));
				menuScene->Reset();
				star::SceneManager::GetInstance()->SetActiveScene(_T("MenuScene"));
			}
		}
#endif
	}
	else
	{
#ifdef _WIN32
		if(m_CooldownTimer < 0)
		{
			if((INPUT_MANAGER->IsKeyboardKeyDown('W', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('W', false)) ||
				(INPUT_MANAGER->IsKeyboardKeyDown('Z', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('Z', false)))
			{
				//SetHighScore
				uint64 score = GlobalVariables::GetInstance()->GetScore();
				uint64 highScore = GlobalVariables::GetInstance()->GetHighScore(); 
				if(highScore < score)
				{
					GlobalVariables::GetInstance()->SetHighScore(score);
				}
				//Reset the Game
				ResetGame();
			}

			if(INPUT_MANAGER->IsKeyboardKeyDown('X', true) &&
				!INPUT_MANAGER->IsKeyboardKeyDown('X', false))
			{
				//SetHighScore
				uint64 score = GlobalVariables::GetInstance()->GetScore();
				uint64 highScore = GlobalVariables::GetInstance()->GetHighScore(); 
				if(highScore < score)
				{
					GlobalVariables::GetInstance()->SetHighScore(score);
				}
				MenuScene* menuScene = reinterpret_cast<MenuScene*>(star::SceneManager::GetInstance()->GetScene(_T("MenuScene")));
				menuScene->Reset();
				star::SceneManager::GetInstance()->SetActiveScene(_T("MenuScene"));
			}
		}
		m_CooldownTimer -= static_cast<float>(context.mTimeManager->GetSeconds());
#endif	
	}	
}


void MainScene::Draw()
{		
	glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT); //Clear the colour buffer
}

void MainScene::ResetGame()
{
	m_pGridObj->ResetGrid();
	m_pSelectorObj->Reset();
	//[TODO]
	//Reset the selector to 0.0 or where-ever you want it to be
	//Reset the line
	m_pGameOverScreen->SetDisabled(true);
	m_pSelectorObj->Freeze(false);
	m_pGridObj->Freeze(false);
	m_bIsGameOver = false;
	m_bIsPaused = false;
	m_pPauseScreen->Reverse();
	m_pHUD->SetDisabled(false);
	GlobalVariables::GetInstance()->SetScore(0);
}
